﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Controls;
using System.ComponentModel;

namespace MemoBoss
{
    public class AppSettings : INotifyPropertyChanged
    {
        public AppSettings()
        {

            try
            {
                settings = IsolatedStorageSettings.ApplicationSettings;
            }
            catch (System.IO.IsolatedStorage.IsolatedStorageException e)
            {
                // handle exception
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        // Our isolated storage settings
        IsolatedStorageSettings settings;

        // The isolated storage key names of our settings
        const string SoundEffectsOnOffKeyName = "SoundEffectsOnOff";
        const string MusicOnOffKeyName = "MusicOnOff";
        const string Pseudo3DOnOffKeyName = "Pseudo3DOnOff";
        const string UseAllImagesKeyName = "UseAllImages";

        public const bool SoundEffectsOnOffDefault = true;
        public const bool MusicOnOffDefault = true;
        public const bool Pseudo3DOnOffDefault = true;
        public const bool UseAllImagesDefault = true;
       
        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (settings.Contains(Key))
            {
                // If the value has changed
                if (settings[Key] != value)
                {
                    // Store the new value
                    settings[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                settings.Add(Key, value);
                valueChanged = true;
            }
            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public T GetValueOrDefault<T>(string Key, T defaultValue)
        {
            T value;

            // If the key exists, retrieve the value.
            if (settings.Contains(Key))
            {
                value = (T)settings[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            settings.Save();
        }

        
            /// <summary>
        /// Property to get and set a CheckBox Setting Key.
        /// </summary>
        public bool UseAllImagesSetting
        {
            get
            {
                return GetValueOrDefault<bool>(UseAllImagesKeyName, UseAllImagesDefault);
            }
            set
            {
                if (AddOrUpdateValue(UseAllImagesKeyName, value))
                {
                    Save();
                    NotifyPropertyChanged("UseAllImagesSetting");
                }
            }
        }
        /// <summary>
        /// Property to get and set a CheckBox Setting Key.
        /// </summary>
        public bool MusicOnOffSetting
        {
            get
            {
                return GetValueOrDefault<bool>(MusicOnOffKeyName, MusicOnOffDefault);
            }
            set
            {
                if (AddOrUpdateValue(MusicOnOffKeyName, value))
                {
                    Save();
                    NotifyPropertyChanged("MusicOnOffSetting");
                }
            }
        }



        /// <summary>
        /// Property to get and set a CheckBox Setting Key.
        /// </summary>
        public bool SoundEffectsOnOffSetting
        {
            get
            {
                return GetValueOrDefault<bool>(SoundEffectsOnOffKeyName, SoundEffectsOnOffDefault);
            }
            set
            {
                if (AddOrUpdateValue(SoundEffectsOnOffKeyName, value))
                {
                    Save();
                    NotifyPropertyChanged("SoundEffectsOnOffSetting");
                }
            }
        }


        /// <summary>
        /// Property to get and set a ListBox Setting Key.
        /// </summary>
        public bool Pseudo3DOnOffSetting
        {
            get
            {
                return GetValueOrDefault<bool>(Pseudo3DOnOffKeyName, Pseudo3DOnOffDefault);
            }
            set
            {
                if (AddOrUpdateValue(Pseudo3DOnOffKeyName, value))
                {
                    Save();
                    NotifyPropertyChanged("Pseudo3DOnOffSetting");
                }
            }
        }
    }
}
